﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="InvokeCommandAction.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   Defines the InvokeCommandAction type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Windows.Input.Commands
{
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Interactivity;

    public class InvokeCommandAction : TriggerAction<FrameworkElement>
    {
        public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand),
            typeof(InvokeCommandAction), null);

        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached("CommandParameter", typeof(object),
            typeof(InvokeCommandAction), null);

        protected override void Invoke(object parameter)
        {
            if (Command != null)
            {
                var routedCommand = Command as RoutedCommand;
                if (routedCommand != null)
                {
                    routedCommand.Execute(AssociatedObject, CommandParameter);
                }
                else
                {
                    Command.Execute(CommandParameter);
                }
            }
        }

        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        public object CommandParameter
        {
            get { return GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }
    }
}
